package com.cskaoyan.mall.search.repository;


import com.cskaoyan.mall.search.entity.ItemDocument;
import org.elasticsearch.action.get.MultiGetRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;
import java.util.Optional;

public interface ProductRepository extends ElasticsearchRepository<ItemDocument, Long> {

    /*
      1. 通过Query注解定义具体的查询字符串
      2. 字符串中的?0是固定格式，表示第0个参数的占位符，在实际查询时会被方法的第一个参数值 title的值替换, 如果有多个参数，依次类推即可
      3. 方法的返回值是List<ItemDocument>
    */
    @Query(
            "{\n" +
                    "    \"match\": {\n" +
                    "      \"title\": \"?0\"\n" +
                    "    }\n" +
                    "  }"
    )
    List<ItemDocument> matchSearch(String title);

    /*
      1. 针对一个查询结果，返回对应的一页数据
      2. Pageable参数是当想要获取分页数据的时候，必须携带的参数，表示分页信息
         比如，查询第多少页数据，每页多少条数据等，该参数不会用来替换我们的@Query字符串中的参数
      3. 返回的结果是一个包含一页文档数据的Page对象
      4. 进行上下限的拼接
 */
    // @Query(
    //         " {" +
    //                 "   \"match\": {\n" +
    //                 "      \"title\": \"?0\"\n" +
    //                 "    }" +
    //                 "}"
    // )
    @Query(
            "{\n" +
                    "    \"bool\": {\n" +
                    "      \"must\": [\n" +
                    "      {\n" +
                    "        \"match\": {\n" +
                    "          \"title\": \"?0\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "      ],\n" +
                    "      \"filter\": {\n" +
                    "        \"range\": {\n" +
                    "          \"price\": {\n" +
                    "            \"gte\": ?1,\n" +
                    "            \"lte\": ?2\n" +
                    "          }\n" +
                    "        }\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }"
    )
    Page<ItemDocument> testSearchPage(String title, Integer priceGt, Integer priceLte, Pageable pageable);

    //  既想实现 高亮 又想实现分页(但是不会返回满足总的文档数量)
    @Query(
            "  {  \"match\": {\n" +
                    "      \"title\": \"?0\"\n" +
                    "    }" +
                    "}"
    )
    @Highlight(
            fields = {
                    @HighlightField(name = "title",
                            parameters = @HighlightParameters(
                                    preTags = "<font color='red'>", postTags = "</font>"))
            }
    )
    List<SearchHit<ItemDocument>> testHighlight(String title, Pageable pageable);

}
